package org.codeminer.parts.server.actions;

/*
 * Copyright (C) 2013 Daniel Wasilew <daniel.wasilew@codeminer.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import org.codeminer.parts.player.Player;
import org.codeminer.parts.player.PlayerInfo;
import org.codeminer.parts.player.PlayerManager;
import org.codeminer.parts.server.permissions.Permission;

import java.util.EnumSet;
import java.util.Set;

public class GetPlayerInfo implements Action {

    private final String VERSION = "3";

    private ActionContext context;

    private final Set<Permission> permissions = EnumSet.noneOf(Permission.class);

    private final Long playerId;

    public GetPlayerInfo(ActionContext context, Long playerId) {
        this.context = context;

        this.playerId = playerId;

        this.permissions.add(Permission.MANAGE_PLAYER);
    }

    @Override
    public String getVersion() {
        return this.VERSION;
    }

    @Override
    public Set<Permission> requiredPermissions() {
        return this.permissions;
    }

    @Override
    public Response execute() {
        PlayerManager playerManager = context.getApplicationPart(PlayerManager.class);
        Player player = playerManager.getPlayer(getPlayerId());
        if (player == null)
            return createPlayerNotFound();
        else
            return createOK(player.getPlayerInfo());

    }

    public Long getPlayerId() {
        return this.playerId;
    }

    private Response createOK(PlayerInfo playerInfo) {
        Response response = new Response("OK");
        response.addProperty("playerInfo", playerInfo);
        return response;
    }

    private Response createPlayerNotFound() {
        Response response = new Response("Error");
        response.addProperty("message.key", "InvalidId");
        response.addProperty("message", "Player with given id not found");
        return response;
    }

}
